<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends MY_Controller {

	public function index(){
		$this->load->view('users');
	}

	public function read(){
		echo "{success: true, users: [{id: 1, name: 'Ed', email: 'ed@sencha.com'}, {id: 2, name: 'Tommy', email: 'tommy@sencha.com'}]}";
	}

	public function save(){
		echo "{success: false, users: [{id: 1, name: 'Ed', email: 'ed@sencha.com'}, {id: 2, name: 'Tommy', email: 'tommy@sencha.com'}]}";
	}

	/*
	 * get users
	 */
	public function getUsers(){

		$this->load->model('user');
		$results = $this->user->getUsers();

		$arrayResults = '';
		foreach ($results as $key => $row){
			if($row->is_user){
				$arrayResults[$key]['text'] = $row->name . ' ' . $row->surname;
				$arrayResults[$key]['id'] = $row->id_node;
				$arrayResults[$key]['leaf'] = 1;
			}
			else{
				$arrayResults[$key]['text'] = $row->name;
				$arrayResults[$key]['id'] = $row->id_node;
				$arrayResults[$key]['leaf'] = 0;
			}

		}

		echo json_encode($arrayResults);
	}

	/*
	 * get user details
	 */
	public function getDetails(){
		$this->load->model('user');
		$results = $this->user->getDetails();
		echo '{success: true, jsonRoot: ' . json_encode($results) . '}';
	}
}